<template>
	<view class="password_pages">
		<view class="login_bk">
			<image mode="widthFix" class="login_bk_img" src="../../static/images/login_bk.png"></image>
		</view>
		<view class="login_box">
			<view class="login_top_box">
				<view class="welcome_text">
					<view class="w_text">欢迎使用</view>
					<image class="text_bk" mode="widthFix" src="/static/images/text_bk.png"></image>
				</view>
				<view class="login_top_text">{{mini_app_name}}</view>
			</view>
			<view style="padding: 0 48rpx;color: #999999;font-size: 34rpx;">重新设置新密码</view>
			<view class="login_input_box">
				<view class="login_input_cell">
					<input class="login_input" :value="mobile" @input="inputMobile" type="number"
						placeholder="请输入手机号" />
				</view>
				<view class="flex-row align-center login_input_cell">
					<view class="flex-1">
						<input class="login_input" :value="captcha" @input="inputCaptcha" type="number"
							placeholder="请输入验证码" />
					</view>
					<view @click="getCode()" :class="ismobile?'code_btn':'code_btn code_btn_def'">{{codeText}}
					</view>
				</view>
				<view class="display_flex login_input_cell">
					<view class="flex-1">
						<input class="login_input" :value="newpassword" @input="inputpassword" type="text"
							:password="showPaw" placeholder="请输入新密码" />
					</view>
					<!-- <uni-icons v-if="showPaw" @click="showPaw=false" type="eye-slash" size="30"></uni-icons>
					<uni-icons v-else @click="showPaw=true" type="eye" size="30"></uni-icons> -->
				</view>
			</view>
			<view class="login_btn_box" @click="goLogin">
				<image class="block_btn_bk" mode="widthFix" src="../../static/images/btn_bk.png"></image>
				<view class="login_btn_text"> 确 定</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		getCode,
		checkCode,
		changePassword
	} from '@/common/api.js'
	let app = getApp()
	let timer;
	export default {
		data() {
			return {
				newpassword: '',
				mobile: '',
				captcha: '',
				codeText: '获取验证码',
				ismobile: true,
				isSubmit: true,
				checked: false,
				isshowPop: false,
				mini_app_name: '',
				showPaw: true
			}
		},
		onShow() {
			if (uni.getStorageSync('initinfo')) {
				this.mini_app_name = uni.getStorageSync('initinfo').mini_app_name
			}
		},
		methods: {
			inputMobile(e) {
				this.mobile = e.detail.value
			},
			inputCaptcha(e) {
				this.captcha = e.detail.value
			},
			inputpassword(e) {
				this.newpassword = e.detail.value
			},
			getCode() {
				if (this.ismobile) {
					if ((/^1[3456789]\d{9}$/).test(this.mobile)) {
						let second = 60;
						if (this.isSubmit) {
							this.isSubmit = false
							getCode({
								mobile: this.mobile,
								event: 'resetpwd'
							}).then(res => {
								if (res.code == 1) {
									this.isSubmit = true
									timer = setInterval(() => {
										second--;
										if (second) {
											this.ismobile = false
											this.codeText = second + "s后重新获取"
										} else {
											clearInterval(timer);
											this.codeText = "获取验证码"
											this.ismobile = true
										}
									}, 1000);
								} else {
									this.isSubmit = true
									uni.showToast({
										title: res.msg,
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})

						}
					} else {
						uni.showToast({
							title: '请输入正确的手机号',
							icon: 'none',
							duration: 1500
						});
					}

				}
			},
			goLogin() {
				var that = this
				that.checked = true
				if (!that.mobile) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none',
						duration: 1500
					});
					return false;
				}
				if (!(/^1[3456789]\d{9}$/).test(that.mobile)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 1500
					});
					return false;
				}
				if (!that.captcha) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 1500
					});
					return false;
				}
				if (!that.newpassword) {
					uni.showToast({
						title: '请输入新密码',
						icon: 'none',
						duration: 1500
					});
					return false;
				}
				if (that.newpassword < 6 || that.newpassword > 30) {
					uni.showToast({
						title: '新密码长度6至30位',
						icon: 'none',
						duration: 1500
					});
					return false;
				}
				if (that.isSubmit) {
					that.isSubmit = false
					checkCode({
						mobile: that.mobile,
						captcha: that.captcha,
						event: 'resetpwd'
					}).then(res => {
						if (res.code == 1) {
							changePassword({
								mobile: that.mobile,
								captcha: that.captcha,
								newpassword: that.newpassword
							}).then(res => {
								if (res.code == 1) {
									uni.showToast({
										title: '设置成功',
										mask: true,
										icon: 'none',
										duration: 1500,
										success() {
											that.isSubmit = true
											uni.navigateBack({
												delta: 1
											})
										}
									})

								} else {
									that.isSubmit = true
									uni.showToast({
										title: res.msg,
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})
						} else {
							that.isSubmit = true
							uni.showToast({
								title: res.msg,
								mask: true,
								icon: 'none',
								duration: 1500
							})
						}
					})
				}

			},
			showPop(type) {
				this.isshowPop = type
			},
			goWechatLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>

<style lang="scss">
	.flex-1 {
		flex:1
	}
	.password_pages {
		overflow: hidden;

		.login_bk {
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 1;

			.login_bk_img {
				width: 100%;
				height: 100%;
			}
		}

		.login_box {
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 2;

			.login_top_box {
				margin-top: 150rpx;
				display: flex;
				align-items: flex-end;
				color: #020204;
				padding: 36rpx;
				letter-spacing: 2rpx;

				.welcome_text {
					width: 250rpx;
					height: 70rpx;
					position: relative;

					.w_text {
						font-size: 52rpx;
						font-weight: bold;
						text-align: center;
						position: absolute;
						left: 0;
						right: 0;
						bottom: 0;
						z-index: 3;
					}

					.text_bk {
						width: 240rpx;
						position: absolute;
						left: 0;
						right: 0;
						bottom: 0;
						z-index: 2;
					}
				}

				.login_top_text {
					font-size: 52rpx;
				}
			}

			.login_logo {
				width: 204rpx;
				height: 204rpx;
			}

			.login_input_box {
				padding: 140rpx 48rpx 48rpx 48rpx;

				.login_input_cell {
					margin-bottom: 50rpx;
					border-bottom: 2rpx solid #BBBBBB;

					.login_input {
						font-size: 30rpx;
						padding: 28rpx 0;
						letter-spacing: 4rpx;
					}
				}

				.code_btn {
					color: #83C100;
					font-size: 24rpx;
					padding: 12rpx 20rpx;
					border-radius: 100rpx;
					letter-spacing: 2rpx;
					border: 2rpx solid #BBBBBB;
				}

				.code_btn_def {
					color: #9A9A9A;
				}
			}

			.login_btn_box {
				height: 88rpx;
				margin: 30rpx 48rpx 0 48rpx;
				position: relative;

				.block_btn_bk {
					width: 100%;
					height: 88rpx;
				}

				.login_btn_text {
					color: #ffffff;
					font-size: 28rpx;
					letter-spacing: 4rpx;
					position: absolute;
					left: 0;
					right: 0;
					top: 0;
					bottom: 0;
					display: flex;
					align-items: center;
					justify-content: center;
					z-index: 4;

					.weixin_img {
						width: 40rpx;
						height: 32rpx;
						margin-top: 4rpx;
					}
				}
			}

			.agreement_box {
				padding: 20rpx 40rpx 0 40rpx;
				color: #666666;
				display: flex;
				align-items: center;
				font-size: 24rpx;
			}

		}

		.mobile_btn_block {
			margin: 30rpx 48rpx 0 48rpx;
			height: 88rpx;
			font-size: 28rpx;
			letter-spacing: 4rpx;
			border-radius: 100px;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #e8e8e8;

			.weixin_img {
				width: 40rpx;
				height: 32rpx;
				margin-top: 4rpx;
			}
		}
	}
</style>